package com.chinaunicom.emergency.filter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author liliang
 * @date 2019-12-15
 */
public class ResponseWrapper extends HttpServletResponseWrapper {
    private ResponsePrintWriter writer;
    private SelfServletOutputStream out;
    public ResponseWrapper(HttpServletResponse response) {
        super(response);
    }
    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        //一定要先判断当前out为空才能去新建out对象，否则一次请求会出现多个out对象
        if (out == null){
            out = new SelfServletOutputStream(super.getOutputStream());
        }
        return out;
    }
    @Override
    public PrintWriter getWriter() throws IOException {
        //一定要先判断当前writer为空才能去新建writer对象，否则一次请求会出现多个writer对象
        if (writer == null){
            writer = new ResponsePrintWriter(super.getWriter());
        }
        return writer;
    }
    public ResponsePrintWriter getMyWriter() {
        return writer;
    }
    public SelfServletOutputStream getMyOutputStream(){
        return out;
    }
}
